from fastapi import Request

from service import UserService, SongService
from common.result.Result import Result
from common.enum import BaseEnum
from util.log import Log
from util.str import Str
from util.token import Token

async def GetILikeSongList(req: Request):
    try:
        token = req.headers.get("token")
        if Token().verify_token(token) == None:
            return Result.error(BaseEnum.TOKEN_INVALID_ERROR)
        
        user = await UserService.getUserByToken(token)
        if user == None:
            return Result.error(BaseEnum.USER_NOT_EXISTS_ERROR)

        songList = await SongService.getILikeSongList(user.id)
        res = []
        for item in songList:
            tmp = dict(item)
            tmp.update(isFavorite=True)
            res.append(tmp)

        return Result.success(res)
    except Exception as e:
        Log.error('获取 song 收藏列表异常', e)
        return Result.error(BaseEnum.SERVICE_ERROR)



